Beginner questions about npth

drichd at tutanota.com drichd at tutanota.com
Tue Sep 1 05:10:02 CEST 2015


I came across to a post about npth
http://lists.gnupg.org/pipermail/gnupg-ru/2012-August/000376.html

Then I did some experiments with it like the example. But I am  not an 
experienced C programmer so I have a few beginner questions:
- Is the example the right way or order to use npth calls (init, create, 
join, exit, etc.)?- How can I utilize multiple cores when I want several 
threads to run on different cpu? - Is npth implemented with call/cc?
Thanks 
========== Example ==========#include <npth.h>#include <string.h>#include 
<stdlib.h>#include <stdio.h>
typedef struct data {  int no;  char *payload;} message;
int new_message(message **msg, int no, char *str);void print_message(void 
*m);
int main(int argc, char** argv) {  message *msg1 = NULL, *msg2 = NULL;   
npth_attr_t t_attr1, t_attr2;  npth_t thread1, thread2;  char *str1 = 
"hello", *str2 = "world!";
  new_message(&msg1, 1, str1);  new_message(&msg2, 2, str2);
  npth_init ();  npth_attr_init(&t_attr1);  npth_create(&thread1, &t_attr1, 
(void *)&print_message, (void *)msg1);  npth_attr_init(&t_attr2);  
npth_create(&thread2, &t_attr2, (void *)&print_message, (void *)msg2);  
npth_join(thread1, (void *)&t_attr1);  npth_join(thread2, (void *)&t_attr2);
  free(msg1);  free(msg2);  return 0;}
int new_message(message **msg, int no, char *str) {  if(msg && *msg == NULL) 
{    *msg = malloc(sizeof(**msg));     if(NULL == *msg) {      printf("Can't 
allocate message.\n");      return -1;    }  }  if(NULL == (*msg)->payload) 
{    (*msg)->payload = malloc(sizeof(str)+1);    if(NULL == (*msg)->payload) 
{      printf("Can't allocate message payload\n");      free(*msg);      
return -1;    }  }  strncpy((*msg)->payload, str, strlen(str));  (*msg)->no = 
no;  return 0;}
void print_message(void *m) {  message *msg;  msg = (message *) m;  
printf("Thread %d owns %s\n", msg->no, msg->payload);  npth_exit(0); }

-------------- next part --------------
An HTML attachment was scrubbed...
URL: </pipermail/attachments/20150901/03da0be4/attachment-0001.html>


More information about the Gnupg-devel mailing list