{"id":19436442,"url":"https://github.com/beached/future_process","last_synced_at":"2025-02-25T06:46:47.837Z","repository":{"id":149955328,"uuid":"151903978","full_name":"beached/future_process","owner":"beached","description":"Future's based on process model","archived":false,"fork":false,"pushed_at":"2019-09-23T03:27:08.000Z","size":57,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-01-07T21:12:38.418Z","etag":null,"topics":["cpp","future","multiprocess"],"latest_commit_sha":null,"homepage":null,"language":"C++","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/beached.png","metadata":{"files":{"readme":"readme.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2018-10-07T03:52:32.000Z","updated_at":"2023-12-08T21:05:44.000Z","dependencies_parsed_at":"2023-05-05T06:19:31.910Z","dependency_job_id":null,"html_url":"https://github.com/beached/future_process","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/beached%2Ffuture_process","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/beached%2Ffuture_process/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/beached%2Ffuture_process/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/beached%2Ffuture_process/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/beached","download_url":"https://codeload.github.com/beached/future_process/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":240619430,"owners_count":19830204,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["cpp","future","multiprocess"],"created_at":"2024-11-10T15:11:10.905Z","updated_at":"2025-02-25T06:46:47.795Z","avatar_url":"https://github.com/beached.png","language":"C++","funding_links":[],"categories":[],"sub_categories":[],"readme":"A set of process/ipc classes and functions to allow for a C++ like experience.  So far tested on Linux(gcc 8.3.0/clang 8.0) and Mac(Apple Clang 10.0.1/gcc 9.1)\n## Future Process\n\nCreate processes and return data via a ```std::future```.  With the only restriction being that the return type is trivial and that exceptions do not traverse the processes.  If an exception is thrown, a runtime_error will be thrown.\n\n```cpp\n#include \u003cdaw/daw_future_process.h\u003e\n\nauto func = []( int b ) {\n\tstd::cout \u003c\u003c \"process\\n\";\n\treturn b * b;\n};\n\nstd::future\u003cint\u003e f1 = daw::process::async( func, 5 );\nstd::future\u003cint\u003e f2 = daw::process::async( func, 10 );\n\nreturn f1.get( ) + f2.get( );\n```\n\nThe above example will create two child processes.  async( Function, Args... ) returns a std::future.\n\n## Process\n\nFork a child process and run a function.  This is analagous to a ```std::thread``` in interface and functionality.  \n\n```cpp\n#include \u003cdaw/daw_process.h\u003e\n\nauto proc = daw::process::fork_process( []( int t ) {\n\tsleep( t )\n}, 5 );\n\nputs( \"Waiting for child to complete\\n\" );\nproc.join( );\nputs( \"Child completed\\n\" );\n```\n\n## Semaphore\n\nA semaphore that allows post, wait, and try_wait operations.\n\n```cpp\n#include \u003cdaw/daw_process.h\u003e\n#include \u003cdaw/daw_semaphore.h\u003e\n\nauto sem_a = daw::process::semaphore( );\nauto sem_b = daw::process::semaphore( );\n\nauto proc = daw::process::fork_process( [\u0026]( unsigned int t ) {\n\twhile( true ) {\n\t\tputs( \"child: sleeping\\n\" );\n\t\tsleep( t );\n\t\tputs( \"child: awake\\n\" );\n\t\tsem_b.post( );\n\t\tputs( \"child: awaiting parent acknowledgement\\n\" );\n\t\tsem_a.wait( );\n\t\tputs( \"child: got parent's acknowledgement\\n\" );\n\t}\n}, 2 );\n\nwhile( true ) {\n\tputs( \"parent: awaiting child\\n\" );\n\tsem_b.wait( );\n\tputs( \"parent: got child's post\\n\" );\n\tsem_a.post( );\n\tputs( \"parent: sent child's post\\n\" );\n}\n```\n\n## Channel\n\nA way to send and ackknowledge a message has been both sent and recieved.\n\n```cpp\n#include \u003cdaw/daw_channel.h\u003e\n#include \u003cdaw/daw_process.h\u003e\n\nauto chan = daw::process::channel\u003cunsigned int\u003e( );\n\nauto proc = daw::process::fork_process( [\u0026chan]( unsigned int t ) {\n\twhile( true ) {\n\t\tputs( \"child: sleeping\\n\" );\n\t\tsleep( t );\n\t\tputs( \"child: awake\\n\" );\n\t\tchan.write( t );\n\t}\n}, 2 );\n\nwhile( true ) {\n\tputs( \"parent: awaiting child\\n\" );\n\tauto val = chan.read( );\n\tassert( val == 2 );\n\tputs( \"parent: got child's post\\n\" );\n}\n```\n\n## String Channel\n\nSimilar to channel but for transferring string like things\n\n```cpp\nauto chan = daw::process::string_channel( );\nstatic constexpr std::string_view message = \"This is a long string test, how about that eh! Hello World.\";\n\nauto proc = daw::process::fork_process( [\u0026chan]( unsigned int t ) {\n    while( true ) {\n        puts( \"child: sleeping\\n\" );\n        sleep( t );\n        puts( \"child: awake\\n\" );\n        chan.write( message );\n    }\n}, 2 );\n\nwhile( true ) {\n    puts( \"parent: awaiting child\\n\" );\n    std::string val = chan.read( );\n    std::cout \u003c\u003c \"parent: message from child '\" \u003c\u003c val \u003c\u003c \"'\\n\";\n    assert( val == message );\n}\n```\n\n## Collection Channel\nSend multiple trivial types over a channel.  When reading a ```std::vector\u003cT\u003e``` is returned.\n\n```cpp\n#include \u003cdaw/daw_process.h\u003e\n#include \u003cdaw/daw_collection_channel.h\u003e\n\nstatic std::vector\u003cint\u003e mul( std::vector\u003cint\u003e vec, int multiplier ) {\n\tfor( int \u0026 item: vec ) {\n\t\titem *= multiplier;\n\t}\n\treturn vec;\n}\n\nint main( ) {\n\tauto chan = daw::process::collection_channel\u003cint\u003e( );\n\tstatic std::vector\u003cint\u003e const message = { 2, 5, 6, 7 };\n\n\tauto proc = daw::process::fork_process(\n\t  [\u0026chan]( unsigned int t ) {\n\t  \tint multiplier = 0;\n\t\t  while( true ) {\n\t\t\t  puts( \"child: sleeping\\n\" );\n\t\t\t  sleep( t );\n\t\t\t  puts( \"child: awake\\n\" );\n\t\t\t  chan.write( mul( message, multiplier++ ) );\n\t\t  }\n\t  },\n\t  2 );\n\n\tint multiplier = 0;\n\twhile( true ) {\n\t\tputs( \"parent: awaiting child\\n\" );\n\t\tauto val = chan.read( );\n\t\tputs( \"parent: message from child \" );\n\t\tassert( val == mul( message, multiplier++ ) );\n\t}\n}\n```\n\n## Shared Mutex\nAn interprocess mutex that acts like ```std::mutex```.  It can be used with items like ```std::lock_guard```\n\n```cpp\n#include \u003cdaw/daw_process.h\u003e\n#include \u003cdaw/daw_shared_mutex.h\u003e\n\nauto mut = daw::process::shared_mutex( );\nauto lck = std::unique_lock( mut );\n\nauto proc = daw::process::fork_process(\n\t[]( daw::process::shared_mutex mtx ) {\n\t\tputs( \"child: about to wait\\n\" );\n\t\tauto lck2 = std::lock_guard( mtx );\n\t\tputs( \"child: awake\\n\" );\n\t},\n\tmut );\n\nsleep( 2 );\nputs( \"parent: about to wake child\\n\" );\nlck.unlock( );\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbeached%2Ffuture_process","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbeached%2Ffuture_process","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbeached%2Ffuture_process/lists"}