Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

not work with thread #23

Open
yjp211 opened this issue Jul 9, 2015 · 4 comments
Open

not work with thread #23

yjp211 opened this issue Jul 9, 2015 · 4 comments

Comments

@yjp211
Copy link

yjp211 commented Jul 9, 2015

I use wslay with thread pool in c, and I got a problem。
To simplify the problem, as the fork-echoserv example:

struct wslay_event_msg *g_msg = NULL;

void echo_data (wslay_event_context_ptr ctx){
    printf("--->echo data!!!\n");
    wslay_event_queue_msg(ctx, g_msg);
}

void on_msg_recv_callback(wslay_event_context_ptr ctx,
                          const struct wslay_event_on_msg_recv_arg *arg,
                          void *user_data)
{
  /* Echo back non-control message */
  if(!wslay_is_ctrl_frame(arg->opcode)) {
      struct wslay_event_msg *msgarg = malloc(sizeof(struct wslay_event_msg));
      msgarg->opcode = arg->opcode;
      msgarg->msg = arg->msg;
      msgarg->msg_length = arg->msg_length;

       g_msg = msgarg;

       //this is work !!
       //echo_data(ctx); 

       //but this not
       pthread_t thread;
       pthread_create(&thread, NULL, (void *)echo_data, (void *)ctx);
  }
}

use thread, package can't be send.

。。。。。。
I just find this problem only in OSX, it's work in Linux

@tatsuhiro-t
Copy link
Owner

I think working well in linux is just coincidence. wslay has no mutex inside it, and using wslay_event_context_ptr from multiple threads is unsafe, and must be avoided.
If some work must be done in the different thread, all required data must be copied, and get passed to the new thread. Just copying pointer value is not enough. You have to make new buffer of size arg->msg_length, and copy the data pointed by arg->msg of length arg->msg_length to the new buffer, and pass it to the thread.

@yjp211
Copy link
Author

yjp211 commented Jul 10, 2015

This example is just trying to show that the asynchronous call to write in recv_callback does not send a packet at OSX.

Such copy data and the use of global variables in this example, just lazy : )

@xsjqqq123
Copy link

hello , I meed the same problem, I want to send lots of data, but wslay_event_queue_msg seems to work only in main thread. Is there any idea?

@tatsuhiro-t
Copy link
Owner

wslay_event_context must not be used by multiple thread at the same time. If you'd like to use it from multiple thread, use mutex around the calls.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants