การใช้ช่องทาง C บริสุทธิ์
ช่องทางที่ไม่ได้บัฟเฟอร์ให้ทั้งกลไกสำหรับการสื่อสารและการซิงโครไนซ์ เมื่อข้อมูลถูกส่งเข้าไปในช่องผู้ส่งจะบล็อกจนกว่าตัวรับสัญญาณจะพร้อม ในทำนองเดียวกันผู้รับจะบล็อกจนกว่าผู้ส่งจะพร้อม
#include <pthread.h>
#include <stdio.h>
#include "chan.h"
chan_t * chan ;
void * ping ()
{
// Send blocks until receiver is ready.
chan_send ( chan , "ping" );
return NULL ;
}
int main ()
{
// Initialize unbuffered channel.
chan = chan_init ( 0 );
pthread_t th ;
pthread_create ( & th , NULL , ping , NULL );
// Receive blocks until sender is ready.
void * msg ;
chan_recv ( chan , & msg );
printf ( "%sn" , msg );
// Clean up channel.
chan_dispose ( chan );
} ด้วยช่องทางที่ไม่ได้รับการยกเว้นผู้ส่งและตัวรับสัญญาณจะถูกซิงโครไนซ์ดังนั้นโปรแกรมข้างต้นจะพิมพ์ ping
แชนเนลบัฟเฟอร์ยอมรับจำนวนที่ จำกัด โดยไม่มีตัวรับสัญญาณที่สอดคล้องกันสำหรับค่าเหล่านั้น การส่งข้อมูลจะไม่บล็อกเว้นแต่ช่องทางเต็ม การรับข้อมูลจะบล็อกเฉพาะในกรณีที่ช่องว่าง
#include <stdio.h>
#include "chan.h"
int main ()
{
// Initialize buffered channel with a capacity of 2.
chan_t * chan = chan_init ( 2 );
// Send up to 2 values without receiver.
chan_send ( chan , "buffered" );
chan_send ( chan , "channel" );
// Later receive the values.
void * msg ;
chan_recv ( chan , & msg );
printf ( "%sn" , msg );
chan_recv ( chan , & msg );
printf ( "%sn" , msg );
// Clean up channel.
chan_dispose ( chan );
} โปรแกรมด้านบนจะพิมพ์ buffered แล้ว channel การส่งไม่บล็อกเนื่องจากช่องมีความจุ 2 ส่งมากขึ้นหลังจากนั้นจะบล็อกจนกว่าจะได้รับค่า
เมื่อช่องถูกปิดจะไม่สามารถส่งค่าได้อีกต่อไป การรับในช่องปิดจะส่งคืนรหัสบ่งชี้ว่าช่องถูกปิด สิ่งนี้จะเป็นประโยชน์ในการสื่อสารความสมบูรณ์ของผู้รับช่อง หากช่องปิดถูกบัฟเฟอร์ค่าจะได้รับจนกว่าจะว่าง
#include <pthread.h>
#include <stdint.h>
#include <stdio.h>
#include "chan.h"
chan_t * jobs ;
chan_t * done ;
void * worker ()
{
// Process jobs until channel is closed.
void * job ;
while ( chan_recv ( jobs , & job ) == 0 )
{
printf ( "received job %dn" , ( int ) job );
}
// Notify that all jobs were received.
printf ( "received all jobsn" );
chan_send ( done , "1" );
return NULL ;
}
int main ()
{
// Initialize channels.
jobs = chan_init ( 5 );
done = chan_init ( 0 );
pthread_t th ;
pthread_create ( & th , NULL , worker , NULL );
// Send 3 jobs over the jobs channel then close it.
int i ;
for ( i = 1 ; i <= 3 ; i ++ )
{
chan_send ( jobs , ( void * ) ( uintptr_t ) i );
printf ( "sent job %dn" , i );
}
chan_close ( jobs );
printf ( "sent all jobsn" );
// Wait for all jobs to be received.
chan_recv ( done , NULL );
// Clean up channels.
chan_dispose ( jobs );
chan_dispose ( done );
}โปรแกรมนี้จะพิมพ์:
sent job 1
received job 1
sent job 2
received job 2
sent job 3
received job 3
sent all jobs
received all jobs
เลือกคำสั่งเลือกชุดของการส่งหรือรับที่เป็นไปได้จะดำเนินการต่อ พวกเขายังให้วิธีการส่งและรับที่ไม่ปิดกั้น การเลือกมีประโยชน์อย่างยิ่งสำหรับการสื่อสารแบบมัลติเพล็กซ์ในหลายช่องทาง
#include <stdio.h>
#include "chan.h"
chan_t * messages ;
chan_t * signals ;
int main ()
{
// Initialize channels.
messages = chan_init ( 0 );
signals = chan_init ( 0 );
void * msg ;
// This is a non-blocking receive. If a value is available on messages,
// select will take the messages (0) case with that value. If not, it will
// immediately take the default case.
switch ( chan_select ( & messages , 1 , & msg , NULL , 0 , NULL ))
{
case 0 :
printf ( "received message %sn" , msg );
break ;
default :
printf ( "no message receivedn" );
}
// A non-blocking send works similarly.
msg = "foo" ;
switch ( chan_select ( NULL , 0 , NULL , & messages , 1 , & msg ))
{
case 0 :
printf ( "sent message %sn" , msg );
break ;
default :
printf ( "no message sentn" );
}
// We can use multiple cases above the default clause to implement a
// multi-way non-blocking select. Here we attempt non-blocking receives on
// both messages and signals.
chan_t * chans [ 2 ] = { messages , signals };
switch ( chan_select ( chans , 2 , & msg , NULL , 0 , NULL ))
{
case 0 :
printf ( "received message %sn" , msg );
break ;
case 1 :
printf ( "received signal %sn" , msg );
break ;
default :
printf ( "no activityn" );
}
// Clean up channels.
chan_dispose ( messages );
chan_dispose ( signals );
}โปรแกรมนี้จะพิมพ์:
no message received
no message sent
no activity