obgm / libcoap

A CoAP (RFC 7252) implementation in C
Other
778 stars 421 forks source link

Multi-threaded CoAP Server using libcoap #567

Open ghost opened 3 years ago

ghost commented 3 years ago

I want to implement a multi-threaded server which is capable of handling CoAP request/response. Is there any guide available on how to achieve the same?

mrdeep1 commented 3 years ago

libcoap is not currently multi-threaded safe, so you can only have one thread managing all access to a coap context (coap_context_t) and all the structures that hang off that (e.g. coap_session_t).

That said, there is nothing wrong with thread A managing context A, Thread B managing a completely different context B and so-on.

There is nothing stopping you doing all the libcoap processing in one thread and pass the requests / responses in a thread safe manner across to other threads.

navneet96 commented 1 year ago

Can I implement (POSIX)multithreading for sending one command in multiple server?

mrdeep1 commented 1 year ago

At present, access to/usage of the same coap_context_t* structures is not thread safe, along with access to/usage of any structures hanging off coap_context_t, some major examples being `coap_session_tandcoap_resource_t*`.

Providing that there is multi-thread protection access to anything to any functions prefixed by coap_, then you would be fine.

Alternatively, having 2 different coap_context_t* is fine providing only one thread access the firstcoap_context_t* and another thread accesses the second coap_context_t*.

There has been some work done on trying to make libcoap multi-thread safe, but it is not that straight forward and has not been completed.

mrdeep1 commented 7 months ago

libcoap (if compiled appropriately) is now multi-thread safe when using the develop branch.