|
|
1.1 root 1: /**********************************************************************
2: * bndbufc.c -- sample program demonstrating two client threads
3: * producing to and consuming from a central buffer
4: * pool via RPC.
5: *
6: * Frederick Chong
7: * Microsoft Developer Support
8: * Copyright (c) 1992, 1993 Microsoft Corporation
9: *
10: ***********************************************************************/
11:
12:
13: #include <windows.h>
14: #include <stdio.h>
15: #include <string.h>
16: #include <stdlib.h>
17: #include <rpc.h>
18: #include "common.h"
19: #include "bndbuf.h"
20:
21: // Function prototypes
22:
23: void producer();
24: void consumer();
25: void produce_item(char * string);
26: void _CRTAPI1 main(int argc, char *argv[]);
27:
28: // Global variables
29:
30: HANDLE hMainThrd; // Handle to main thread
31: HANDLE hConThrd; // Handle to consumer thread
32: HANDLE hProdThrd; // Handle to producer thread
33: LONG lConThreadId; // Consumer thread ID
34: LONG lProdThreadId; // Producer thread ID
35: DWORD retCode;
36:
37:
38: /**********************************************************************
39:
40: void _CRTAPI1 main(int argc, char *argv[])
41:
42: Creates the producer and consumer thread and then suspend itself.
43:
44: ***********************************************************************/
45:
46: void _CRTAPI1 main(int argc, char *argv[])
47: {
48:
49:
50: // Create Consumer Thread
51:
52: hConThrd = CreateThread(NULL,
53: 0,
54: (LPTHREAD_START_ROUTINE)consumer,
55: NULL,
56: 0,
57: (LPDWORD) &lConThreadId );
58:
59:
60: // Create producer thread
61:
62: hProdThrd = CreateThread(NULL,
63: 0,
64: (LPTHREAD_START_ROUTINE)producer,
65: NULL,
66: 0,
67: (LPDWORD) &lProdThreadId );
68:
69:
70: // Suspend the main thread
71:
72: hMainThrd = GetCurrentThread();
73:
74: if (SuspendThread(hMainThrd) == -1)
75: {
76: retCode = GetLastError();
77: printf("\nSuspendThread returns %d\n", retCode);
78: ExitProcess(0);
79: }
80:
81:
82:
83:
84: }
85:
86:
87: /***********************************************************************
88:
89: void producer()
90:
91: This is the producer thread function. It produces a string of 20 random
92: characters then make a RPC to insert the string item.
93:
94: ************************************************************************/
95:
96: void producer()
97: {
98: char item[MAX_SIZE];
99:
100: // Delay for awhile before starting
101: Sleep(200);
102:
103: while (TRUE)
104: {
105: produce_item(item); // Produce random string
106:
107:
108: // RPC guarded statements
109:
110: RpcTryExcept
111: {
112: insert_buffer(item); // Put item in buffer
113: printf("\nproduce ---> %s", item);
114: }
115: RpcExcept(1)
116: {
117: printf("The RPC runtime library raised exception 0x%lx.\n", RpcExceptionCode());
118: printf("Please verify that the server application and \n");
119: printf("the locator service have been started.");
120: }
121: RpcEndExcept
122:
123: // End RPC guarded statements
124:
125: }
126: }
127:
128:
129: /***********************************************************************
130:
131: void consumer()
132:
133: This is the consumer thread function. It makes a RPC and then remove
134: the string item from the central buffer pool.
135:
136: ************************************************************************/
137:
138:
139: void consumer()
140: {
141: char item[20];
142:
143: while (TRUE)
144: {
145:
146: // RPC guarded statements
147:
148: RpcTryExcept
149: {
150: remove_buffer(item); // Take item from buffer
151: printf("\nconsume ---> %s", item);
152: }
153:
154: RpcExcept(1)
155: {
156: printf("The RPC runtime library raised exception %lx.\n", RpcExceptionCode());
157: printf("Please verify that the server application and \n");
158: printf("the locator service have been started.");
159: }
160: RpcEndExcept
161:
162: // End RPC guarded statements
163:
164:
165: }
166:
167:
168: }
169:
170:
171: /***********************************************************************
172:
173: void produce_item(char *string)
174:
175: Generates a random string of 20 characters.
176:
177: ************************************************************************/
178:
179: void produce_item(char *string)
180: {
181: char *tmp;
182: int i;
183:
184: tmp = string;
185:
186: for (i = MAX_SIZE; i > 1; i--)
187: {
188: *tmp = (rand() % 26) + 'A';
189: tmp++;
190: }
191: strcpy(tmp, "\0");
192:
193: }
194:
195:
196:
197: /***********************************************************************
198: MIDL allocate and free
199: ***********************************************************************/
200:
201: void __RPC_FAR * __RPC_API MIDL_user_allocate(size_t len)
202: {
203: return(malloc(len));
204: }
205:
206: void __RPC_API MIDL_user_free(void __RPC_FAR * ptr)
207: {
208: free(ptr);
209: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.