По моему идентифицировать издателей/подписчиков лучше всего по имени. И пусть имя они сами себе выбирают.
#include <stdio.h>
#include <unistd.h>
#include <pthread.h>
#include <stdlib.h>
#include "publisher.h"
#include "screen.h"
#include "pubs.h"
#include "trade.h"
#include "chat.h"
static void free_data ( void *event, void *data ) {
struct trade *td = ( struct trade * ) data;
if ( td ) {
free ( td );
delete_all_publisher ( FREE );
}
}
void *handler ( void *data ) {
while ( 1 ) {
sleep ( 1 );
struct trade *td = calloc ( 1, sizeof ( struct trade ) );
for ( int i = 0; i < 2; i++ ) {
td->trade[i] = i;
}
td->size = 2;
send_event ( TRADE, td );
init_publisher ( FREE, free_data, td );
}
}
int main ( ) {
init_screen ( );
init_chat ( );
pthread_t pt;
pthread_create ( &pt, NULL, handler, NULL );
while ( 1 ) {
show_screen_trade ( );
print_chat ( );
send_event ( FREE, NULL );
sleep ( 1 );
}
}
#ifndef __TRADE_H
#define __TRADE_H
struct trade {
int size;
int trade[10];
};
#endif
#ifndef __PUBS_H
#define __PUBS_H
enum { TRADE, FREE };
#endif
#include <stdio.h>
#include "publisher.h"
#include "pubs.h"
#include "trade.h"
static struct trade *td;
static void in_screen ( void *event, void *data ) {
td = ( struct trade * ) event;
}
void init_screen ( ) {
init_publisher ( TRADE, in_screen, NULL );
}
void show_screen_trade ( ) {
if ( !td ) return;
for ( int i = 0; i < td->size; i++ ) {
printf ( "скидка %d%%\n", td->trade[i] );
}
td = NULL;
}
#include <stdio.h>
#include "publisher.h"
#include "pubs.h"
#include "trade.h"
static struct trade *td;
static void in_chat ( void *event, void *data ) {
td = ( struct trade * ) event;
}
void init_chat ( ) {
init_publisher ( TRADE, in_chat, NULL );
}
void print_chat ( ) {
if ( !td ) return;
for ( int i = 0; i < td->size; i++ ) {
printf ( "чат: скидка %d%%\n", td->trade[i] );
}
td = NULL;
}